﻿Namespace Settings

    Public Class TextSetting
        Inherits BaseSetting


        Public Sub New(ByVal id As SettingKey,
                       ByVal name As String,
                       ByVal desc As String,
                       ByVal val As Object)
            MyBase.New(id, name, desc, val)
        End Sub

        Protected Overrides Function getUIElement() As Control
            Dim tb = New TextBox
            tb.Text = Me.Value
            AddHandler tb.PreviewLostKeyboardFocus, AddressOf textChangeHandler
            Return tb
        End Function

        Protected Overrides Sub resetUIElement()
            CType(Me.UIElement, TextBox).Text = Me.Value.ToString
        End Sub

        Protected Overrides Sub validateValue(ByVal value As Object)
        End Sub

        Protected Overrides Function getValueFromString(ByVal value As String) As Object
            Return value
        End Function

        Private Sub textChangeHandler(ByVal sender As Object, ByVal e As KeyboardFocusChangedEventArgs)
            Try
                Me.Value = CType(sender, TextBox).Text
            Catch ex As ArgumentOutOfRangeException
                If (MsgBox(ex.Message, MsgBoxStyle.OkCancel, "Invalid Value") = MsgBoxResult.Cancel) Then
                    resetUIElement()
                End If
                ' Prevent focus from moving
                e.Handled = True
            End Try
        End Sub


    End Class

End Namespace

